<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controller library
jimport('joomla.application.component.controller');
 
/**
 * Retail Shop Component Controller
 */
class RetailShopControllerMyShop extends JController
{
    function display(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		$view = $this->getView('MyShop','html');
		$shop_id = 0;
		
        if(isset($_GET['shop_id'])){			
			$shop_id = $_GET['shop_id'];
		}else{			
			if($_SESSION['__default']['user']->id != 0){
				$id_user = $_SESSION['__default']['user']->id;
				$shop_id = $model->checkusershop($id_user);
				$view->assign('main',true);
				if($shop_id == 0)
                {					
                    header('Location: index.php?option=com_retailshop&view=home&task=startretailshop');
                }
				//$getallshopbyUser = $model->getallshopbyUser($id_user);
				//$view->assign('getallshopbyUser',$getallshopbyUser);	
			}else{
				header('Location: index.php?option=com_users&view=login');
			}
		}	
        
		if($_SESSION['__default']['user']->id != 0 && isset($_GET['shop_id'])){
			$id_user = $_SESSION['__default']['user']->id;
			$shop_id = $model->checkusershop($id_user);
			if($shop_id == $_GET['shop_id']){
				$view->assign('main',true);
			}
		}
		
		$shop = $model->getShop($shop_id);
		$slideshow = $model->getArrSlideshow($shop_id);		
		$products = $model->getArrProducts($shop_id);		
		$deals = $model->getArrProducts($shop_id,0,true);
		$events = $model->getArrEvent($shop_id);
		$catagorys = $model->getCategoryProduct($shop_id);			
		$categoryMenus = $model->getCategoryMenuOfShop($shop_id);
		$catagory = $model->getCategory();
        if(isset($getallshopbyUser))
        {
            $view->assign('catagorys',$catagory);
             $eventsAll = $model->getArrAllEvent();
             $view->assign('eventsAll',$eventsAll);
             $catagorys_Menu = $model->getCategory();
              $view->assign('catagorys_Menu',$catagorys_Menu);
        }		
        else
        {
       	    $view->assign('shop',$shop);
    		$view->assign('products',$products);
    		$view->assign('deals',$deals);
    		$view->assign('events',$events);
    		$view->assign('categorys',$catagorys);
            $view->assign('categoryMenus',$categoryMenus);
            $view->assign('shop_id',$shop_id);
            $view->assign('slideshow',$slideshow);            
        }	
		$this->sendmailreport();
			
		$view->setLayout('default');
		$view->display();
    }
     
	function sendmailreport(){
		if(isset($_POST['btnreport'])){			
			$from = 'retailshop.report@info.com';
			$recipient = 'phongtran255@gmail.com';
			$fromname = 'Retailshop Report';
			$subject = 'Report website adult inappropriate content!';
			$body = $_POST['txtcontent'];			
			$body .= '<br />link: ('.$this->curPageURL().')';
			JUtility::sendMail($from, $fromname, $recipient, $subject, $body, true);
		}	
	}
	function setting($sh = 0)
	{
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		
		$id_user = $_SESSION['__default']['user']->id;		
		$id_shop = $model->checkusershop($id_user);
		if(isset($id_shop)) {
			$_SESSION['shopid'] = $id_shop;
			$shop_id = $id_shop;
			$products = $model->getArrProducts($shop_id,0,false,0,true);
			$events = $model->getArrEvent($shop_id);
			$catolog =  $model->getCategoryProduct($shop_id);
			$slideshow = $model->getArrSlideshow($shop_id);
			$view = $this->getView('MyShop','html');
			$categoryMenus = $model->getCategoryMenuOfShop($shop_id);
			
			$view->assign('products',$products);
			$view->assign('events',$events);
			$view->assign('catolog',$catolog);
			$view->assign('slideshow',$slideshow);
			$view->assign('categoryMenus',$categoryMenus);
            $view->setLayout('setting');
            
			$view->setting();
		}		
	}
    
	function newcategory()
	{	
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');		
		$view = $this->getView('MyShop','html');
		if(isset($_SESSION['shopid'])){
			$shopid = $_SESSION['shopid'];
		}else
			$shopid = $_GET['shop'];
		
		$menu = $model->getCategoryProduct($shopid);		
		$view->assign('menu',$menu);
		$view->setLayout('addcatagory');
		$view->addcatagory();
	}
	function editcatelogy()
	{	
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');		
		$view = $this->getView('MyShop','html');
		if(isset($_SESSION['shopid'])){
			$shopid = $_SESSION['shopid'];
		}else
			$shopid = $_GET['shop'];
		$id = $_GET['id'];
		$menu = $model->getCategoryProduct($shopid);		
		$cate = $model->getCategoryProductbyid($id);		
		$view->assign('menu',$menu);
		$view->assign('cate',$cate);
		$view->setLayout('addcatagory');
		$view->addcatagory();
	}
	
	function addnew()
	{	
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		$view = $this->getView('MyShop','html');		
		
		if(isset($_SESSION['shopid'])){
			$shop_id = $_SESSION['shopid'];
		}else
			$shop_id = getRequest('shop');
		if(isset($_GET['id'])){
			$id = getRequest('id');
			$product =  $model->getProduct($id);
			$relatedproduct = $model->getArrRelatedProduct($id);
			$catologproduct = $model->getcategory_related_product($id);
			
			$view->assign('product',$product);
			$view->assign('relatedproduct',$relatedproduct);
			$view->assign('catologproduct',$catologproduct);
		};
		$products =  $model->getArrProducts($shop_id);					
		$catolog =  $model->getCategoryProduct($shop_id);
		
		
		$view->assign('catolog',$catolog);
		$view->assign('products',$products);
		$view->setLayout('add');
		$view->add();
	}
	function editslideshow(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		$view = $this->getView('MyShop','html');	
		
		$id = $_GET['id'];
		$slide = $model->getSlideshow($id);
		
		$view->setLayout('addslideshow');
		$view->assign('slide',$slide);
		$view->addslideshow();
	}
	function addslideshow(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		$view = $this->getView('MyShop','html');		
		
		$view->setLayout('addslideshow');		
		$view->addslideshow();
	}
	function save(){
		$mainframe = JFactory::getApplication();		
		if(isset($_POST)){
			$model = $this->getModel('RetailShop');			
			$idshop = 0;
			if(isset($_POST['shop_id'])){
				if(isset($_SESSION['shopid']))					
					$idshop = $_SESSION['shopid'];
				else
					$idshop = $_POST['shop_id'];
				$model->saveProduct($idshop);
			}else {
				$model->saveProduct();
			}
		}
		$this->setting($idshop);
	}
	
	function newevent(){
		$mainframe = JFactory::getApplication();
		$model_myshop = $this->getModel('RetailShop');
					
		$view = $this->getView('MyShop','html');
		
		$view->setLayout('addevent');
		$view->addevent();
	}
	
	function editevnet(){
		$mainframe = JFactory::getApplication();
		$model_myshop = $this->getModel('RetailShop');
		
		$id = getRequest('id');	
		$event =  $model_myshop->getEvent($id);	
		
		$view = $this->getView('MyShop','html');
		$view->assign('event',$event);
		
		$view->setLayout('addevent');
		$view->addevent();
	}
	
	function saveevent(){
		$mainframe = JFactory::getApplication();
		if(isset($_POST)){
			$model_myshop = $this->getModel('RetailShop');					
			if(isset($_SESSION['shopid'])){				
				$model_myshop->saveEvent($_SESSION['shopid']);
			}
		}
		$this->setting();		
	}
	
	function limit(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		
		$shop_id = $_SESSION['shopid']; 
		$limit = getRequest('limit');	
		$page = 1;
		if(isset($_POST['page'])){
			$page = $_POST['page'];
		}
		$arrproduct = $model->getArrProducts($shop_id, $limit, false, $page - 1,true);		
		$return = '';		
		foreach($arrproduct as $product){
			$return .='
			<tr>
				<td width="5%"><input type="checkbox" value="'.$product->id.'" name="ckbproduct[]"/></td>
				<td>
					<a href="index.php?option=com_retailshop&view=myshop&task=addnew&id='.$product->id.'">'.$product->name.'</a></td>
				<td>'.$product->price.'</td>
				<td>'.$product->promotion.'</td>
			</tr>';
		}	
	echo $return; 
    $mainframe->close();
	}
	
	function limitevent(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');		
		$shop_id = $_SESSION['shopid'];	
		$limit = getRequest('limit');
		$page = 1;
		if(isset($_POST['page'])){
			$page = $_POST['page'];
		}
		$arrproduct = $model->getArrEvent($shop_id, $limit, $page - 1);
		$return = '';
		foreach($arrproduct as $event){
			$return .='
			<tr>
				<td><input type="checkbox" title="Check" value="'.$event->id.'" name="ckbevent[]"></td>
				<td>'.$event->title.'</td>
				<td>'.$event->content.'</td>
				<td>'.$event->start_date.'</td>
				<td>'.$event->end_date.'</td>
			</tr>';
		}	
	echo $return; 
    $mainframe->close();
	}
	
	function delete(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');		
		$id = getRequest('id');
		$name = getRequest('name');		
		switch($name){
			case 'event':
				$model->deleteEvent($id);
				break;
			case 'product':
				$model->deleteProduct($id);
				break;
			case 'catelogy':
				$model->deleteCategoryProduct($id);
				break;
			case 'slideshow':
				$model->deleteSlideshow($id);
				break;
			default:
				break;
		}
		$this->setting();
	}
	
	function productdetail(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		$id = getRequest('id');			
		$product = $model->getProduct($id);
		$productrelate = $model->getArrRelatedProduct($id);
		$countrys = $model->getArrCountry();
		$shop_id = getRequest("shop_id");
        $catagorys = $model->getCategoryProduct($shop_id);
        $listcomment = $model->listcomment($id);
       	$shop = $model->getShop($shop_id);				
		$events = $model->getArrEvent($shop_id);
        $categoryMenus = $model->getCategoryMenuOfShop($shop_id);
		$view = $this->getView('MyShop','html');
        $view->assign('shop',$shop);			
		$view->assign('events',$events);
        $view->assign('catagorys',$catagorys);
		$view->setLayout('productdetail');
		$view->assign('product',$product);
		$view->assign('productrelate',$productrelate);
		$view->assign('countrys',$countrys);
		$view->assign('listcomment',$listcomment);
		$view->assign('categoryMenus',$categoryMenus);
		if(isset($_POST['btncallme'])){
			$from = $_POST['txtemail'];
			$fromname = $_POST['txtname'];
			$recipient = $model->getShopmailbyproduct($product->id);//'phongtran255@gmail.com';
			$subject = 'Call me back!';
			$body = '<h3>Customer Want To Order: </h3><br />';
			$body .= ' - Customer name: '.$_POST['txtname'];
			$body .= ' - Customer email: '.$_POST['txtemail'];
			$body .= ' - Customer mobile: '.$_POST['txtmobile'];
			$body .= ' - Product: '.$_POST['txtproductname'];
			//var_dump($recipient);
			JUtility::sendMail($from, $fromname, $recipient, $subject, $body, true);
		}
		
		$view->addevent();
	}
	function addcomment(){			
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');		
		$title = trim(getRequest('titlecomment'));
		$email = trim(getRequest('emailcomment'));
		$name = trim(getRequest('namecomment'));
		$content = trim(getRequest('review_field'));
		
		$id = $_GET['value'];
		
		if($model->addComment($title, $email, $name, $content, $id ))
			echo "true";
		else
			echo "false";
		$mainframe->close();
	}
	
	function saveslide(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		$action = $_POST['action'];
		switch($action){
			case 'addnew':
				$model->saveslideshow();
				break;
			case 'edit':				
				$model->editSlideshow();
				break;
			default:
				break;
		}		
		$this->setting();		
	}
	function products(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		$view = $this->getView('MyShop','html');		
		$view->setLayout('products');
		$id = $_GET['idp'];
		$shop_id = 0;
	        if(isset($_GET['shop_id']))
			$shop_id = $_GET['shop_id'];
		else{	
			if($_SESSION['__default']['user']->id != 0){
				$id_user = $_SESSION['__default']['user']->id;
				$shop_id = $model->getShopId($id_user);
				$shop_id = $shop_id->id;	
			}else{
				header('Location: index.php?option=com_users&view=login');
			}
		}		
		$shop = $model->getShop($shop_id);				
		$events = $model->getArrEvent($shop_id);
		$catagorys = $model->getCategoryProduct($shop_id);			
	        $categoryMenus = $model->getCategoryMenuOfShop($shop_id);
		$view->assign('shop',$shop);			
		$view->assign('events',$events);
		$view->assign('catagorys',$catagorys);
		$view->assign('shop_id',$shop_id);
		$listproducts  = $model->productbycatelogy($id);		
		$view->assign('listproducts',$listproducts);
        	$view->assign('categoryMenus',$categoryMenus);
			
		$this->sendmailreport();
		$view->products();
	}
	function limitproduct(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		$url = JURI::base().'images/com_retailshop/';
		$shop_id = getRequest('shop_id');		
		$page = getRequest('page');		
		$deals = getRequest('deals')=='true' ? true : false;
		$arrproduct = $model->getproductpage($shop_id, $page - 1, 6, $deals);		
		$return = '';
		$i = 0;
		foreach($arrproduct as $product){
			$price = $product->price - $product->promotion*$product->price/100;
			$class = ++$i%3==0?' class="last" ':'';
			$img_url = ($product->image_url =='')?$url.'noimages.jpg':$url.$product->image_url;
			$return .= '<li'.$class.'><a href="index.php?option=com_retailshop&view=myshop&task=productdetail&shop_id='.$shop_id.'&id='.$product->id.'"><center><img src="'.$img_url.'" width="200" height="170" /></center></a>
			<h2 class="product-name">
			<a href="index.php?option=com_retailshop&view=myshop&task=productdetail&shop_id='.$shop_id.'&id='.$product->id.'">'.$product->name.'</a>
			</h2>
			<div class="price-box">
				<p class="price-new">
					<span class="price-label">Price:'.number_format($price,2).'</span> $
					<span id="product-price" class="price">'.number_format($product->price,2).'</span> $
				</p>
			</div>';
			if($deals) {
				$dateAviable = $product->timetime;
				$return .= '<div style="'.$dateAviable.'" class="clock" id="clock_'.$product->id.'">
										  <p>
											<span id="hours_'.$product->id.'"></span>
											Hours
										  </p>
										  <div class="space">:</div>
										  <p>
											<span id="minutes_'.$product->id.'"></span>
											Minutes
										  </p>
										  <div class="space">:</div>
										  <p>
											<span id="seconds_'.$product->id.'"></span>
											Seconds
										  </p>
									</div>';
			}
			$return .= '<div class="free-gift">
			<span></span>
			</div>
			</li>';
		}
	echo $return; 
    $mainframe->close();
	}
	function limitalldeal(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		$url = JURI::base().'images/com_retailshop/';
		
		$page = getRequest('limit');		
		$deals = getRequest('deals')=='true' ? true : false;
		$arrproduct = $model->listdeals($page-1);		
		$return = '';
		$i = 0;
		foreach($arrproduct as $deals){			
			$class = ++$i%3==0?' class="last" ':'';		
			$dateAviable = $deals->timetime;		
			$img_url = ($deals->image_url =='')?$url.'noimages.jpg':$url.''.$deals->image_url;
			$return .= '<li '.$class.'>
									<a href="index.php?option=com_retailshop&amp;view=myshop&amp;task=productdetail&amp;id='.$deals->id.'"><img width="200" height="170" src="'.$img_url.'"></a>
									<h2 class="product-name">
										<a href="index.php?option=com_retailshop&amp;view=myshop&amp;task=productdetail&amp;id='.$deals->id.'">'.$deals->name.'</a>
									</h2>
									<div class="price-box">
										<p class="price-new">
											<span class="price-label">Price:</span>
											<span id="product-price" class="price"><s>'.number_format($deals->price,2).' $</s> - '.number_format($deals->sale,2).' $</span>
										</p>
									</div>
									<div style="'.$dateAviable.'" class="clock" id="clock_'.$deals->id.'">
										  <p>
											<span id="hours_'.$deals->id.'"></span>
											Hours
										  </p>
										  <div class="space">:</div>
										  <p>
											<span id="minutes_'.$deals->id.'"></span>
											Minutes
										  </p>
										  <div class="space">:</div>
										  <p>
											<span id="seconds_'.$deals->id.'"></span>
											Seconds
										  </p>
									</div>
								</li>';
			
		}
	echo $return; 
    $mainframe->close();
	}
	function limitproduct2(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		$url = JURI::base().'images/com_retailshop/';
		
		$page = getRequest('limit');	
		$id = $_GET['value'];
		$arrproduct = $model->productbycatelogy($id, $page-1);		
		$return = '';
		$i = 0;
		foreach($arrproduct as $deals){			
			$class = ++$i%3==0?' class="last" ':'';		
			$dateAviable = $deals->timetime;		
			$img_url = ($deals->image_url =='')?$url.'noimages.jpg':$url.''.$deals->image_url;
			$return .= '<li '.$class.'>
									<a href="index.php?option=com_retailshop&amp;view=myshop&amp;task=productdetail&amp;id='.$deals->id.'"><img width="200" height="170" src="'.$img_url.'"></a>
									<h2 class="product-name">
										<a href="index.php?option=com_retailshop&amp;view=myshop&amp;task=productdetail&amp;id='.$deals->id.'">'.$deals->name.'</a>
									</h2>
									<div class="price-box">
										<p class="price-new">
											<span class="price-label">Price:</span>
											<span id="product-price" class="price"><s>'.number_format($deals->price,2).' $</s> - '.number_format($deals->sale,2).' $</span>
										</p>
									</div>
									<div style="'.$dateAviable.'" class="clock" id="clock_'.$deals->id.'">
										  <p>
											<span id="hours_'.$deals->id.'"></span>
											Hours
										  </p>
										  <div class="space">:</div>
										  <p>
											<span id="minutes_'.$deals->id.'"></span>
											Minutes
										  </p>
										  <div class="space">:</div>
										  <p>
											<span id="seconds_'.$deals->id.'"></span>
											Seconds
										  </p>
									</div>
								</li>';
			
		}
	echo $return; 
    $mainframe->close();
	}
	function savecate(){
		$mainframe = JFactory::getApplication();		
		if(isset($_POST)){
			$model = $this->getModel('RetailShop');			
			$action = $_POST['action'];			
			$idshop = 0;
			if($action == 'edit'){
				$idshop = $_SESSION['shopid'];
				$model->savecate($idshop);
			}else {
				$model->savecate();
			}
		}
		$this->setting($idshop);
	}
	
	function defaultshop(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		$view = $this->getView('MyShop','html');
		
		//get id default shop
		$shop_id = $model->getdefaulftshop();
		
		//get shop info
		$shop = $model->getShop($shop_id);
		
		//get all products
		$products = $model->getArrProducts($shop_id);		
		
		//get all deals
		$deals = $model->getArrProducts($shop_id,0,true);
		
		//get all event
		$events = $model->getArrEvent($shop_id);
		$slideshow = $model->getArrSlideshow($shop_id);
		//get all catagorys
		$catagorys = $model->getCategoryProduct($shop_id);			
		
		$categoryMenus = $model->getCategoryMenuOfShop($shop_id);
		
		$view->assign('shop',$shop);
		$view->assign('products',$products);
		$view->assign('deals',$deals);
		$view->assign('events',$events);
		$view->assign('categorys',$catagorys);
        $view->assign('categoryMenus',$categoryMenus);
        $view->assign('shop_id',$shop_id);
        $view->assign('slideshow',$slideshow);
		
		$view->setLayout('default');
		$view->display();
	}
	function randomshop(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		$view = $this->getView('MyShop','html');				 		
		//get shop info
		$shop = $model->getrandomshop();
		$shop_id = $shop->id;
		//get all products
		$products = $model->getArrProducts($shop_id);		
		
		//get all deals
		$deals = $model->getArrProducts($shop_id,0,true);
		
		//get all event
		$events = $model->getArrEvent($shop_id);
		$slideshow = $model->getArrSlideshow($shop_id);
		//get all catagorys
		$catagorys = $model->getCategoryProduct($shop_id);			
		
		$categoryMenus = $model->getCategoryMenuOfShop($shop_id);
		
		$view->assign('shop',$shop);
		$view->assign('products',$products);
		$view->assign('deals',$deals);
		$view->assign('events',$events);
		$view->assign('categorys',$catagorys);
        $view->assign('categoryMenus',$categoryMenus);
        $view->assign('shop_id',$shop_id);
        $view->assign('slideshow',$slideshow);
		
		$view->setLayout('default');
		$view->display();
	}

	function curPageURL() {
		$pageURL = 'http';
		if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
		$pageURL .= "://";
		if ($_SERVER["SERVER_PORT"] != "80") {
			$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		} else {
			$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		}
		return $pageURL;
	}
	
	function eventdetail(){
		$mainframe = JFactory::getApplication();
		$model = $this->getModel('RetailShop');
		$view = $this->getView('MyShop','html');		
		$view->setLayout('products');
		$id = $_GET['idp'];
		$shop_id = 0;
	        if(isset($_GET['shop_id']))
			$shop_id = $_GET['shop_id'];
		else{	
			if($_SESSION['__default']['user']->id != 0){
				$id_user = $_SESSION['__default']['user']->id;
				$shop_id = $model->getShopId($id_user);
				$shop_id = $shop_id->id;	
			}else{
				header('Location: index.php?option=com_users&view=login');
			}
		}		
		$shop = $model->getShop($shop_id);				
		$events = $model->getArrEvent($shop_id);
		$catagorys = $model->getCategoryProduct($shop_id);			
	    $categoryMenus = $model->getCategoryMenuOfShop($shop_id);
		$event = $model->getEvent($id);
		
		$view->assign('shop',$shop);			
		$view->assign('events',$events);
		$view->assign('catagorys',$catagorys);
		$view->assign('shop_id',$shop_id);		
        $view->assign('categoryMenus',$categoryMenus);
        $view->assign('event',$event);
			
		$this->sendmailreport();
		$view->products();
	}
	
}